package com.xone.xapp;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class BannerItemUserActivity extends BasicActivity {
	protected final String TAG = BannerItemUserActivity.class.getSimpleName();
    protected LayoutInflater mInflater;
	@Override
	public String getHeaderTitle() {
		return "我的广告详情";
	}
	@Override
	public int getContentView() {
		return R.layout.banner_item_user;
	}
	
	protected void onCreate(Bundle savedInstanceState) {
		Bundle bundle = this.getIntent().getExtras();
		String id = (null == bundle) ? "" : bundle.getString("id");
		super.onCreate(savedInstanceState);
		if (!ContextUtils.isOnline(this)) {
			return;
		}
		if (null == id) {
			Toast.makeText(this, "缺少请求参数", Toast.LENGTH_SHORT).show();
			this.finish();
			return;
		}
		LinearLayout view = (LinearLayout)findViewById(R.id.banner_item_details_zone);
		showProgress();
		this.mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		new InitDataTask(this, view, ContextUtils.getScreenWidth(this)).execute(id);
	}
	
	public class InitDataTask extends AsyncTask<String, Integer, JSONObject> {
		protected Context context;
		protected LinearLayout view;
		protected int imageWidth;
		protected ImageLoader imageLoader = null;
		public InitDataTask(Context context, LinearLayout view, int imageWidth) {
			this.context = context;
			this.imageWidth = imageWidth;
			this.view = view;
			imageLoader = new ImageLoader(context, imageWidth * 2);
		}
		@Override
		protected JSONObject doInBackground(final String... params) {
			if (isCancelled()) {
				return null;
			}
			List<NameValuePair> reqparams = new ArrayList<NameValuePair>();
			reqparams.add(new BasicNameValuePair("adbanner.id", params[0]));
			return HttpUtils.httpGet(Constants.APP_URL + "/adbanner/itemJson.html", reqparams, 
					new HttpUtils.onGetSuccessListener<JSONObject>() {
						@Override
						public JSONObject onSuccess(int status, String result) {
							if (HttpUtils.SC_OK == status) {
								JSONObject jsonObject = JSONUtils.getJSONObjectFromString(result);
								JSONObject banner = JSONUtils.getJSONObjectForKey(jsonObject, "adbanner");
								return banner;
							} else {
								Toast.makeText(context, "Status Code:" + status, Toast.LENGTH_SHORT).show();
							}
							return null;
						}
					}, context);
		}
		@Override
		protected void onPostExecute(JSONObject result) {
			super.onPostExecute(result);
			if (null == result) {
				return;
			}
			JSONObject banner = result;
			//设置数据体
			ViewTag viewTag = new ViewTag();
			viewTag.setId(JSONUtils.getStringFromJSONObject(banner, "id"));
			viewTag.setAdType(JSONUtils.getStringFromJSONObject(banner, "adType"));
			viewTag.setRefId(JSONUtils.getStringFromJSONObject(banner, "refId"));
			String imageId = JSONUtils.getStringFromJSONObject(banner, "adRefId");
			//更新按钮的操作
			View link = view.findViewById(R.id.bannerLink);
			link.setTag(viewTag);
			link.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					linkAction((ViewTag)v.getTag());
				}
			});
			int childCount = view.getChildCount();
			for (int i = 0 ; i < childCount; i++) {
				View v = view.getChildAt(i);
				String tag = (String)v.getTag();
				if (v instanceof LinearLayout && null != tag) {
					String value = JSONUtils.getStringFromJSONObject(banner, tag);
					if (null != value) {
						TextView tv = (TextView)view.findViewById(context.getResources().getIdentifier(tag, "id", context.getPackageName()));
						if (null != tv) {
							tv.setText(StringUtils.formatStringDate(value));
						}
					}
				}
			}
			View mImageView = mInflater.inflate(R.layout.show_image, null);
			ImageView imageView = (ImageView)mImageView.findViewById(R.id.show_image_0);
			imageLoader.displayImage(Constants.APP_URL + "/assistant/image.html?id=" + imageId, imageView);
//				imageView.getLayoutParams().height = imageWidth;
			view.addView(mImageView, 2);
			hideProgress();
		}
	}
	
	/**
	 * 点击更新按钮的事件
	 * @param tag
	 */
	protected void linkAction(final ViewTag tag) {
		String type = tag.getAdType();
		Intent intent = null;
		if ("0".equals(type)) {//求购
			intent = new Intent(BannerItemUserActivity.this, ProductItemActivity.class);
		} else if ("1".equals(type)) {
			intent = new Intent(BannerItemUserActivity.this, PurchaseItemActivity.class);
		} else if ("2".equals(type)) {
			intent = new Intent(BannerItemUserActivity.this, CompanyItemActivity.class);
		}
		intent.putExtra("id", tag.getRefId());
		startActivity(intent);
	}
	
	protected class ViewTag {
		protected String id;
		protected String adType;//广告类型
		protected String refId;//广告对应的链接id
		public String getId() {
			return id;
		}
		public void setId(String id) {
			this.id = id;
		}
		public String getAdType() {
			return adType;
		}
		public void setAdType(String adType) {
			this.adType = adType;
		}
		public String getRefId() {
			return refId;
		}
		public void setRefId(String refId) {
			this.refId = refId;
		}
	}
}
